package cn.lizemin.exception;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lzm
 * @version 1.0
 * @description: TODO
 * @date 2022/1/9 12:41
 */
@Controller
public class GlobalExceptionResolver implements HandlerExceptionResolver {
    //resolveExcpetion: 当控制器中任意一个方法出现异常时,如果该控制器的方法没有自己异常处理(try...catch),则会进入当前方法
    //注意:在异常处理这个方法中 完成自定义异常处理
    //参数1: request 当前请求对象
    //参数2: response 当前请求对应响应对象
    //参数3: 当前出现错误的方法对象
    //参数4: 出现异常的异常对象
    //返回值: modelAndview 模型和视图
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        System.out.println("全局异常处理。。。");
        System.out.println("当前异常为：" + e);
        ModelAndView modelAndView = new ModelAndView();

        //针对不同异常类型跳转不同页面
        if (e instanceof UserNameNotFoundException) {
            modelAndView.setViewName("error");
            return modelAndView;
        }
        modelAndView.setViewName("500");
        return modelAndView;

    }




}
